﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DynamicIpTool.Service
{
    public partial class ServiceForm : Form
    {
        System.Net.WebClient http = new System.Net.WebClient();
        public ServiceForm()
        {
            InitializeComponent();
        }
        protected void TryStop() {
                WorkerTimer.Stop();
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TryStop();
            WorkerTimer.Interval = Appsettings.Default.RegisterIntervalSec * 1000;
            WorkerTimer.Start();
            startToolStripMenuItem.Checked = true;
            stopToolStripMenuItem.Checked = false;
            DoRegisterAction();
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TryStop();
            startToolStripMenuItem.Checked = false;
            stopToolStripMenuItem.Checked = true;
        }

        private void forceUpdateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DoRegisterAction();
        }
        public void DoRegisterAction() {
            try
            {
                var q = string.Format(Appsettings.Default.RegisterUrl, Appsettings.Default.RegisterKey);
                Log(q);
                var r = http.DownloadString(q);
                Log(r);
                Log("Done.");
            }
            catch (System.Net.WebException we)
            {
                Log(we.Message,2);
                Log("如未发布服务器端，请将[this .exe path]\\RemoteIpQuery.aspx发布到Url对应位置。");
            }
            catch (Exception ex)
            {
                Log(ex.Message,2);
            }
        }
        void Log(string s,int level=0) {
            textBox1.AppendText(string.Format("[{0:MM-dd HH:mm:ss.fff}]\t{1}\r\n",DateTime.Now,s));
            if (level > 1) {
                notifyIcon1.ShowBalloonTip(1000,string.Empty,s, ToolTipIcon.Warning);
            }
        }

        private void WorkerTimer_Tick(object sender, EventArgs e)
        {
            DoRegisterAction();
        }

        private void queryCurrentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                var q = string.Format(Appsettings.Default.QueryUrl, Appsettings.Default.RegisterKey);
                Log(q);
                var r = http.DownloadString(q);
                Log(r);
                Log("Done.");
            }
            catch (Exception ex)
            {
                Log(ex.Message,2);
            }
        }

        private void clearLogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Mail to kevin_geng@163.com to contact me.");

        }

        private void ServiceForm_Load(object sender, EventArgs e)
        {

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Visible = !this.Visible;
            hideWindowToolStripMenuItem.Text = (this.Visible ? "Hide" : "Show") + " window";

        }

        private void hideWindowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Visible = !this.Visible;
            hideWindowToolStripMenuItem.Text = (this.Visible ? "Hide" : "Show") + " window";
        }

        private void quitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
